#include <bits/stdc++.h>
#define int __int128
using namespace std;

int t,n,a[200010],dp1[20010][2],dp2[20010][2];

inline int read(){
	int s=0;
	bool f=0;
	char c=getchar();
	while(!isdigit(c)){
		if(c=='-')f^=1;
		c=getchar();
	}
	while(isdigit(c)){
		s=(s<<1)+(s<<3)+(c^48);
		c=getchar();
	}
	return f==0?s:-s;
}

void write(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10|48);
}

void dp50(){
	for(register int i=1;i<=n;++i){
		memset(dp2,0,sizeof(dp2));
		for(register int j=1;j<i;++j){
			register int l=a[j];
			dp2[l][1]=max(dp2[l][1],dp1[l][1]+(a[i]==a[i-1])*a[i]);
			dp2[l][0]=max(dp2[l][0],dp1[l][0]+(a[i]==a[i-1])*a[i]);
			dp2[a[i-1]][1]=max(dp2[a[i-1]][1],dp1[l][0]+(a[i]==l)*a[i]);
			dp2[a[i-1]][0]=max(dp2[a[i-1]][0],dp1[l][1]+(a[i]==l)*a[i]);
		}
		memcpy(dp1,dp2,sizeof(dp1));
	}
	register int ans=0;
	for(register int i=1;i<=n;++i)
		ans=max(ans,max(dp1[a[i]][1],dp1[a[i]][0]));
	write(ans);
}

signed main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	t=read();
	while(t--){
		n=read();
		for(register int i=1;i<=n;++i)
			a[i]=read();
		dp50();
		puts("");
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
